package server;

import java.io.*;

/* DEV LOG
 * 12/23/09 - Started from complete scratch
 *          - Got basic FileExists function working
 *          -- with folder set up basics done
 *          - Folders work in an array that holds
 *          -- the full path of each folder
 * 12/24/09 - Tried out a Player class
 *          - Set up player name and level arrays, not sure
 *          -- if it can be done better but it works
 * 12/25/09 - Separated Players class into Players.java
 *          - Separated command handling into its own method
 *          - Fixed up -p command so you can set the
 *          -- name and level of any avaliable index
 *          - Commented the folder stuff out since it won't be used
 * 12/26/09 - Added a Socket class, needs a lot of work
 *          - Changed -p command to have the name and level optional
 *          - if you leave them out it just tells you the current
 *          - index's data
 */

class Main {
    public static String[] Folders;
    public static int TotalFolders = 2;
    public static int CurFolder = 0;
    public static boolean serverRunning;

    public static void main(String[] args) {
        System.out.println("Server is initializing...");

        //initFolderArray();
        //initFolder("Accounts");

        Players.initPlayers(10);

        DataInputStream in = new DataInputStream(System.in);

        System.out.println("Server is running...");

        serverRunning = true;
        String aLine;
        
        while (serverRunning) {
            try {
                aLine = in.readLine();
                handleCommand(aLine);
            }
            catch (Exception e) {
            }
        }

        System.out.println("Goodbye!");
        System.exit(-1);
    }
    
    private static void handleCommand(String cmdString) {
        try {
            if (cmdString.substring(0, 2).equals("-p")) {
                try {
                    String[] tempStr;
                    tempStr = cmdString.split(" ");

                    if (tempStr.length == 4) {
                        int tempIndex = Integer.parseInt(tempStr[1]);
                        String tempName = tempStr[2];
                        int tempLevel = Integer.parseInt(tempStr[3]);

                        Players.setName(tempIndex, tempName);
                        Players.setLevel(tempIndex, tempLevel);

                        System.out.println("Index " + tempIndex + " (Name: " + Players.Name(tempIndex) + " | Level: " + Players.Level(tempIndex) + ")");
                    } else if (tempStr.length == 2) {
                        int tempIndex = Integer.parseInt(tempStr[1]);
                        System.out.println("Index " + tempIndex + " (Name: " + Players.Name(tempIndex) + " | Level: " + Players.Level(tempIndex) + ")");
                    } else {
                        System.out.println(tempStr.length + "Error: { Usage: -p (int 0-" + (Players.totalPlayers - 1) + ") |string 'name'| |int level| }");
                    }
                }
                catch (Exception e) {
                    System.out.println("Error: { Usage: -p (int 0-" + (Players.totalPlayers - 1) + ") |string 'name'| |int level| }");
                }
            } else if (cmdString.substring(0, 5).equals("-stop")) {
                serverRunning = false;
            } else {
                System.out.println("Invalid command '" + cmdString.substring(0, 2) + "'.");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /*private static void initFolderArray() {
    *    Folders = new String[TotalFolders];
    *
    *    int i = 0;
    *
    *    while (i < TotalFolders) {
    *        Folders[i] = new String();
    *        i++;
    *    }
    *}
    *
    *private static void initFolder(String folderName) {
    *
    *    try {
    *        File Folder = new File ("." + File.separatorChar + folderName + File.separatorChar);
    *        Folders[CurFolder] = Folder.getCanonicalPath();
    *    }
    *    catch(Exception e) {
    *        e.printStackTrace();
    *    }
    *
    *    if (!FileExists(Folders[CurFolder])) {
    *        System.out.println(folderName + " folder does not exist.");
    *        System.exit(-1);
    *    }
    *
    *    System.out.println(folderName + " folder: " + Folders[CurFolder]);
    *    CurFolder++;
    *}
    *
    *private static boolean FileExists(String FileName) {
    *    File f = new File (FileName);
    *    return f.exists();
    *}
    */
}